<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="jxl.Workbook" %>
<%@ page import="jxl.Sheet" %>
<%@ page import="jxl.Cell" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.cinsea.file.service.FileUpload" %>
<%@ page import="com.cinsea.common.JdbcDao" %>
<%@ page import="com.cinsea.common.Env" %>
<%@ page import="com.cinsea.util.StringUtil" %>
<%@ page import="com.cinsea.common.IDGenerator" %>
<%@ page import="com.cinsea.file.service.FilestoreService" %>
<%@ page import="com.cinsea.file.model.Filestore" %>
<%@ page import="com.cinsea.util.DateUtil" %>
<%@ page import="com.cinsea.util.FileUtil" %>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%
    FileUpload fileUpload = new FileUpload();
    request = fileUpload.resolveMultipart(request);

    String action = request.getParameter("action");
    JdbcDao dao = Env.getBean("jdbcDao");
    if (action.equals("savebudget")) {
        String prjid = request.getParameter("prjid");
        String year = request.getParameter("year");
        List<Map> kemulist = dao.getJdbcTemplate().queryForList("select * from ut_fyys where kmtype is not null and kmtype>0");
        dao.update("delete from ut_prjbudget where prjid=?", new Object[]{prjid});
        dao.update("delete from ut_prjbudgetyear where prjid=? and lf=?", new Object[]{prjid, year});
        for (Map kemu : kemulist) {
            String kemuid = (String) kemu.get("processid");
            String totalbudget = StringUtil.null2String(request.getParameter("t_" + kemuid));
            String yearbudget = StringUtil.null2String(request.getParameter("y_" + kemuid));
            try {
                dao.update("insert into ut_prjbudget(id,prjid,kemu,val)values(?,?,?,?)", new Object[]{IDGenerator.getUnquieID(), prjid, kemuid, totalbudget});
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                dao.update("insert into ut_prjbudgetyear(id,prjid,kemu,lf,val)values(?,?,?,?,?)", new Object[]{IDGenerator.getUnquieID(), prjid, kemuid, year, yearbudget});
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        out.print("success");
        return;
    }

    if (action.equals("import")) {
        String prjid = request.getParameter("prjid");
        ArrayList attachids = fileUpload.getFilestoreList();
        FilestoreService filestoreService = Env.getBean("filestoreService");
        if (attachids.size() == 0) {
            out.print("fail");
        }

        Filestore attach = (Filestore) attachids.get(0);
        InputStream is = new FileInputStream(attach.getObjpath());
        Workbook wb = Workbook.getWorkbook(is);
        try {
            Sheet st = wb.getSheet(0);
            int rowNumber = st.getRows();
            int colNumber = st.getColumns();
            int rowIndex = 0;
            int totalColIndex = 0;
            Map yearColIndexMap = new HashMap();
            String year = DateUtil.getCurrentDate().substring(0, 4);
            List<Map> kemulist = dao.getJdbcTemplate().queryForList("select * from ut_fyys where kmtype>0 order by orderby");
            for (int i = 0; i < rowNumber; i++) {
                for (int j = 0; j < colNumber; j++) {
                    Cell c00 = st.getCell(j, i);
                    String cellName = c00.getContents();
                    if (cellName.equals("总预算")) {
                        rowIndex = i;
                        totalColIndex = j;
                    }
                    if (cellName.equals("本年度预算")) {
                        yearColIndexMap.put(year, j);
                    }
                    if (cellName.startsWith("年度预算(") && cellName.endsWith(")")) {
                        String forYear = cellName.substring(5, 9);
                        yearColIndexMap.put(forYear, j);
                    }
                }
                if (totalColIndex != 0 || yearColIndexMap.keySet().size() > 0) break;
            }
            if (totalColIndex != 0 || yearColIndexMap.keySet().size() > 0) {


                if (totalColIndex != 0) { //导入项目总预算
                    dao.update("delete from ut_prjbudget where prjid=?", new Object[]{prjid});
                    int currentIndex = rowIndex;
                    for (Map kemu : kemulist) {
                        currentIndex++;
                        try {
                            Cell c00 = st.getCell(totalColIndex, currentIndex);
                            String cellName = c00.getContents();
                            dao.update("insert into ut_prjbudget(id,prjid,kemu,val)values(?,?,?,?)", new Object[]{IDGenerator.getUnquieID(), prjid, kemu.get("processid"), cellName});
                        } catch (Exception e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                    }
                }
                if (yearColIndexMap.keySet().size() > 0) { //导入年度预算
                    for (Object key : yearColIndexMap.keySet()) {
                        int yearColIndex = (Integer) yearColIndexMap.get(key);
                        dao.update("delete from ut_prjbudgetyear where prjid=? and lf=?", new Object[]{prjid, key});
                        int currentIndex = rowIndex;
                        for (Map kemu : kemulist) {
                            currentIndex++;
                            try {
                                Cell c00 = st.getCell(yearColIndex, currentIndex);
                                String cellName = c00.getContents();
                                dao.update("insert into ut_prjbudgetyear(id,prjid,kemu,lf,val)values(?,?,?,?,?)", new Object[]{IDGenerator.getUnquieID(), prjid, kemu.get("processid"), key, cellName});
                            } catch (Exception e) {
                                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                            }
                        }
                    }
                }
            }
            out.print("success");
        } finally {
            wb.close();
            is.close();
            FileUtil.deleteFile(attach.getObjpath());
        }


    }
%>